﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using WF_Business;

namespace ExtBusiness.Inspect
{
    public class Inspect
    {
        public  DataTable GetYCInfo(string userid,string strSjlx,string strStartDate,string strEndDate,string s_worktype)
        {
            string strSql = @"select (select user_name from st_user where userid=被告人) 办事人,违规事实,
        decode(事件类型,2,'回退','过期') 事件类型,w.wname 流程名,
        (select w.step from st_work_item w where w.wiid=s.wiid union select h.step from st_work_item_hist h where h.wiid=s.wiid) 节点名,
        to_char(违规时间,'yyyy-mm-dd hh24:mi:ss') 违规时间,id,wiid,s.iid
        from xt_inspect s,st_workflow w, st_instance i
         where 记载状态 is null and 裁决状态 is null and i.isdelete<>1
        and s.iid=i.iid and w.wid=i.wid";

            if (!string.IsNullOrEmpty(strSjlx))
                strSql += " and 事件类型=" + strSjlx;
            if (!string.IsNullOrEmpty(strStartDate))
                strSql += " and to_date(to_char(违规时间,'yyyy-mm-dd'),'yyyy-mm-dd')>=to_date('" + strStartDate + "','yyyy-mm-dd')";
            if (!string.IsNullOrEmpty(strEndDate))
                strSql += " and to_date(to_char(违规时间,'yyyy-mm-dd'),'yyyy-mm-dd')<=to_date('" + strEndDate + "','yyyy-mm-dd')";
            if (string.IsNullOrEmpty(s_worktype))
            {
                s_worktype = "1";
            }
            else if (s_worktype != "1")
            {
                strSql += string.Format(" and wname = '{0}'", s_worktype);
            }

            //登录人员部门
            string SystemManUser = string.Empty;
            if (ConfigurationManager.AppSettings["SystemManUser"] != null)
                SystemManUser = ConfigurationManager.AppSettings["SystemManUser"];

            string JCSUser = string.Empty;
            if (ConfigurationManager.AppSettings["JCSUser"] != null)
                JCSUser = ConfigurationManager.AppSettings["JCSUser"];

            bool isTrue = false;
            User.DepartHandle adh = new User.DepartHandle();
            DataTable dtUserDepart = adh.dtUserDepart(userid);
            for (int i = 0; i < dtUserDepart.Rows.Count; i++)
            {
                //监察室or--6__st_st_department---DEPARTID---6
                if (SystemManUser == dtUserDepart.Rows[i][0].ToString() ||
                    JCSUser == dtUserDepart.Rows[i][0].ToString())
                {
                    isTrue = true;
                    break;
                }
            }

            if (!isTrue)
            {
                strSql += string.Format(@" and w.wname in (select t.task from xt_query_right t where t.userid = '{0}')", userid);
            }

            System.Data.DataTable dtOut;
            SysParams.OAConnection().RunSql(strSql, out dtOut);
            return dtOut;
        }
    }
}
